<?php
/**
 * Created by PhpStorm.
 * User: Hong
 * Date: 2018/3/24
 * Time: 15:33
 * Function:
 */

namespace Admin\Controllers\Administrator;
use Admin\Controllers\BaseAdminController;
use Admin\Resources\DepartmentResources;
use Admin\Resources\RoleResources;
use Illuminate\Http\Request;
use Platform\Models\Admin\Permission;
use Platform\Models\Admin\Department;
use Platform\Repository\Admin\DepartmentRepository;
use Platform\System\Tools\ResultTool;

/**
 * @module 部门管理
 * Class RoleController
 * @package App\Http\Controllers\Admin\System
 */
class DepartmentController extends BaseAdminController
{

    protected $repository;

    public function __construct(DepartmentRepository $repository){
        parent::__construct();
        $this->repository = $repository;
    }

    /**
     * @permission 部门列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $roles =  $this->repository->screen($request);
        return myResult()->setData($roles)->send();
    }

    /**
     * @permission 添加部门
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $data = $this->verify($request);

//        if (isset($data['is_default'])){
//            $this->repository->clearDefault();
//        }

        $department = Department::create($data);

        if ($department && is_array($request->get('permissions'))) {
            $permissionIds = array_filter($request->get('permissions'));
            foreach ($permissionIds as $key => $id){
                $permission = Permission::find($id);
                if (!$permission) unset($permissionIds[$key]);
            }
            $department->permissions()->sync($permissionIds);
        }

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'部门添加成功')->send();
    }

    /**
     * @permission 查看部门信息
     *
     * @param Department $department
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function show(Department $department){

        $department->load('permissions');
        return myResult()->setData($department,DepartmentResources::class)->send();
    }

    /**
     * @permission 更新部门
     *
     * @param Request $request
     * @param Department $department
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(Request $request, Department $department)
    {
        $data = $this->verify($request,$department->id);

        $department->update($data);

        $permissionIds = array_filter($request->get('permissions'));
        if (!empty($permissionIds)) {
            foreach ($permissionIds as $key => $id){
                $permission = Permission::find($id);
                if (!$permission) unset($permissionIds[$key]);
            }
            $department->permissions()->sync($permissionIds);
            Permission::refreshCache();
        }
        return myResult()->setCode(ResultTool::CODE_SUCCESS,'部门编辑成功')->send();
    }

    /**
     * @permission 删除部门
     *
     * @param Department $department
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(Department $department)
    {
        if ($department->isAdmin())
            myResult()->stop(ResultTool::CODE_TARGET_WRONG,'超管角色不可删除');
//        if ($department->is_default)
//            myResult()->stop(ResultTool::CODE_TARGET_WRONG,'默认角色不可删除');

        $department->permissions()->detach();
        $department->admins()->where('department_id',$department->id)->update(['department_id' => null]);
        Permission::refreshCache();
        $department->delete();

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'删除成功')->send();
    }

    /**
     * 数据验证
     * @param Request $request
     * @param null $department_id
     * @return array
     */
    public function verify(Request $request,$department_id = null){
        $data = $request->only(['name','parent_id']);

        if (empty($data['name'])) myResult()->stop(ResultTool::CODE_PARAM_LOST,'名称不可为空');

        if (getStrLength($data['name']) > 20) myResult()->stop(ResultTool::CODE_PARAM_WRONG,'名称过长');

        if ($department_id){
            $department_log = $this->repository->getOneByNameExpectThis($data['name'],$department_id);
        }else{
            $department_log = $this->repository->getOneByName($data['name']);
        }
        if ($department_log) myResult()->stop(ResultTool::CODE_TARGET_WRONG,'部门已存在');

        return $data;
    }
}